package com.tanhua.server.controller;

import com.tanhua.server.service.VideoService;
import com.tanhua.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("smallVideos")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @PostMapping
    public ResponseEntity<Void> saveVideo(@RequestParam("videoThumbnail") MultipartFile picFile,
                                          @RequestParam("videoFile") MultipartFile videoFile) {
        try {
            if (this.videoService.saveVideo(picFile, videoFile)) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping
    public ResponseEntity<PageResult> queryVideoList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        try {
            if (page <= 0) {
                page = 1;
            }
            PageResult pageResult = this.videoService.queryVideoList(page, pageSize);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PostMapping("{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String videoId){
        try {
            Long likeCount = videoService.likeComment(videoId);
            if (likeCount != null) {
                return ResponseEntity.ok(likeCount);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PostMapping("{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String videoId){
        try {
            Long likeCount = videoService.disLikeComment(videoId);
            if (likeCount != null) {
                return ResponseEntity.ok(likeCount);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    @GetMapping("{id}/comments")
    public ResponseEntity<PageResult> queryCommentList(@PathVariable("id") String videoId,
                                                       @RequestParam(value = "page",defaultValue = "1") Integer page,
                                                       @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){

        try {
            PageResult pageResult = videoService.queryCommentList(videoId, page, pageSize);
            return ResponseEntity.ok(pageResult);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    @PostMapping("{id}/comments")
    public ResponseEntity<Void> saveComment(@RequestBody Map<String,String> param,
                                            @PathVariable("id") String videoId){
        try{
            String comment = param.get("comment");
            Boolean bool = videoService.saveComment(videoId,comment);
            if(bool){
                return ResponseEntity.ok(null);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PostMapping("comments/{id}/like")
    public ResponseEntity<Long> commentsLikeComment(@PathVariable("id") String videoCommentId){
        try{
            Long l = videoService.likeComment(videoCommentId);
            if (l != null){
                return ResponseEntity.ok(l);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PostMapping("comments/{id}/dislike")
    public ResponseEntity<Long> disCommentsLikeComment(@PathVariable("id") String videoCommentId){
        try{
            Long l = videoService.disLikeComment(videoCommentId);
            if (l != null){
                return ResponseEntity.ok(l);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PostMapping("{uid}/userFocus")
    public ResponseEntity<Void> followUser(@PathVariable("uid")Long followUserId){
        try {
            Boolean bool = videoService.followUser(followUserId);
            if (bool){
                return ResponseEntity.ok(null);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PostMapping("{uid}/userUnFocus")
    public ResponseEntity<Void> disFollowUser(@PathVariable("uid") Long followUserId){
        try{
            if(videoService.disFollowUser(followUserId)){
                return ResponseEntity.ok(null);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
